package ipsite.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


import java.net.InetAddress;
import java.net.MalformedURLException;

import java.util.ArrayList;
import java.util.Date;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


import ipsite.controller.Utils;
import ipsite.data.Data;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;



public class Whois extends Thread{
	DBObject domain;
	public Whois(DBObject domain)
	{
		this.domain=domain;
		this.start();
	}
	
	public void run()
	{
		try {
//        	BasicDBObject info = new BasicDBObject();
			Process process;
			BufferedReader buf;
			String line;
			String whois="";
			if(!Utils.checkSubDomain(domain.get("name").toString()))
			{
	        	process = Runtime.getRuntime().exec("whois "+domain.get("name").toString());
				buf = new BufferedReader(new InputStreamReader(
				process.getInputStream()));

				while ((line = buf.readLine()) != null) {
					whois += line+"<br/>";
				}
			} else {
				process = Runtime.getRuntime().exec("whois "+Utils.returnSubDomainTo(domain.get("name").toString()));
				buf = new BufferedReader(new InputStreamReader(
				process.getInputStream()));

				while ((line = buf.readLine()) != null) {
					whois += line+"<br/>";
				}
			}
			
			process = Runtime.getRuntime().exec("dig "+domain.get("name").toString());
			buf = new BufferedReader(new InputStreamReader(
			process.getInputStream()));
			
			String dig="";
			while ((line = buf.readLine()) != null) {
				dig += line+"<br/>";
			}
			
			process = Runtime.getRuntime().exec("nslookup "+domain.get("name").toString());
			buf = new BufferedReader(new InputStreamReader(
			process.getInputStream()));
			
			String ns="";
			while ((line = buf.readLine()) != null) {
				ns += line+"<br/>";
			}
			
			process = Runtime.getRuntime().exec("dig "+domain.get("name").toString()+" MX");
			buf = new BufferedReader(new InputStreamReader(
			process.getInputStream()));
			
			int googleMail = 0;
			while ((line = buf.readLine()) != null) {
				if(Pattern.matches("^[\\w.-_ ]*googlemail.com[\\w\\W]*", line))
				{
					googleMail = 1;
				}
			}
			
			String ip = "";
			try {
				InetAddress giriAddress = java.net.InetAddress.getByName(domain.get("name").toString());
				ip = giriAddress.getHostAddress();
			} catch (Exception e) {
				// TODO: handle exception
			}
         
			process = Runtime.getRuntime().exec("whois "+ip);
			buf = new BufferedReader(new InputStreamReader(
			process.getInputStream()));
			
			String wip="";
			while ((line = buf.readLine()) != null) {
				wip += line+"<br/>";
			}
			
			domain.put("whois", whois);
			domain.put("ns", ns);
			domain.put("dig", dig);
			domain.put("ip", ip);
			domain.put("wip", wip);
			domain.put("gmail", googleMail);
			
            String str  = whois;
            
            Pattern p = Pattern.compile("[^ :]+@[^ ]+");

	        Matcher m = p.matcher(str.replaceAll("\\<.*?\\>", " "));
	        ArrayList<String> l_keywords = new ArrayList<String> ();
	        BasicDBObject b_email = new BasicDBObject();
	        while (m.find()) {
	        	if(!l_keywords.contains(m.group().toLowerCase().trim().replaceAll("\\(", "").replaceAll("\\)", "")))
				{
					l_keywords.add(m.group().toLowerCase().trim().replaceAll("\\(", "").replaceAll("\\)", ""));
					b_email.put(m.group().toLowerCase().trim().replaceAll("\\(", "").replaceAll("\\)", "").replaceAll("[.]+", "&&&"), 1);
				}
	        }
	        
	        domain.put("email", b_email);
	        
	        //
//	        p = Pattern.compile("(([\\W]*Name Server[\\W]*|nserver:[\\W]*|Nameservers:[\\W]*|Servers:[\\W]+)[\\w-]+[.]+[\\w-_]+[.]+[\\w-_]+[.]*[\\w-_]*+[.]*[\\w-_]*|[\\w-]+[.]+[\\w-_]+[.]+[\\w-_]+[.]+[\\w-_]+[.]+[\\w-_]+)", Pattern.CASE_INSENSITIVE);
//
//	        m = p.matcher(str.replaceAll("\\<.*?\\>", " "));
//
//	        String save = null;
//	        
//	        ArrayList<String> l_server = new ArrayList<String> ();
//	        BasicDBObject b_server = new BasicDBObject();
//	        
//	        while (m.find()) {
//	        	save = Pattern.compile("([\\W]*Name Server[\\W]*|nserver:[\\W]*|Nameservers:[\\W]*|Servers:[\\W]+)",Pattern.CASE_INSENSITIVE).matcher(m.group()).replaceAll("").trim().toLowerCase();
//	        	if(!l_server.contains(save))
//				{
//	        		l_server.add(save);
//	        		b_server.put(save.replaceAll("[.]+", "&&&"), 1);
//				}
//	        }
//	        
//	        info.put("wserver", b_server);
	        
	        //
//	        p = Pattern.compile("(Creation Date:|Created On:|created:|\\[Created on\\]|Domain Create Date:|registered:|Registration Date:|Entry created:[\\W]+)[ ]*[\\w- \\/:.]+",Pattern.CASE_INSENSITIVE);
//
//	        m = p.matcher(str.replaceAll("\\<.*?\\>", " "));
//
//	        while (m.find()) {
//	        	info.put("created",m.group().replaceAll("(Creation Date:|Created On:|created:|\\[Created on\\]|Created:|Domain Create Date:|registered:|Registration Date:|Created on:|Entry created:[\\W]+)", "").trim().replaceAll("\\/", "-"));
//	        }
//	        
//	        //
//	        p = Pattern.compile("(Expiration Date:|expires:|\\[Expires on\\]|Expire Date:|expire:|paid-till:|last-update:|Renewal date:[\\W]+)[ ]*[\\w- \\/:.]+",Pattern.CASE_INSENSITIVE);
//
//	        m = p.matcher(str.replaceAll("\\<.*?\\>", " "));
//
//	        while (m.find()) {
//	        	info.put("exp",m.group().replaceAll("(Expiration Date:|expires:|\\[Expires on\\]|Expire Date:|expire:|paid-till:|last-update:|Renewal date:[\\W]+)", "").trim().replaceAll("\\/", "-"));	
//	        }
//	        
//	        //
//	        p = Pattern.compile("(ADMINISTRATIVE CONTACT INFO[\\W]+|Registrant:[\\W]*|Registrant Name:[ ]*|Administrative Contact :[\\W]+|person:[\\W]*|\\[Registrant\\][\\W]*|Registrant[\\W]*Name:|registrar:|Domain Owner:[\\W]+)[a-z0-9 ,.\\-\\_]+", Pattern.CASE_INSENSITIVE);
//
//	        m = p.matcher(str.replaceAll("\\<.*?\\>", " "));
//
//	        while (m.find()) {
//	        	save = Pattern.compile("(ADMINISTRATIVE CONTACT INFO[\\W]+|Registrant:[\\W]*|Registrant Name:[ ]*|Administrative Contact :[\\W]+|person:[\\W]*|\\[Registrant\\][\\W]*|Registrant[\\W]*Name:|registrar:|Domain Owner:[\\W]+)",Pattern.CASE_INSENSITIVE).matcher(m.group()).replaceAll("").trim();
//	        	info.put("user", save);
//	        }
	        
//	        DBCollection coll = Data.data("domain");
//
//			BasicDBObject set = new BasicDBObject("$set", info);
//			coll.update(domain, set);
	        

        } catch (MalformedURLException e) {
            // ...
        } catch (IOException e) {
            // ...
        }

	}
//	public static void main( String args[])
//	{
//		new Whois("ketoan.org");
//	}
}
